<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace org;
class Tree {

    /**
     * 无线分类
     * @param array $data 数据
     * @param string $html 标识符
     * @param string $fieldPri id
     * @param string $fieldPid 上级id
     * @param int $uid   级别
     * @param int $level 层级
     * @return array 数组
     */
    static public function Forlevel($data, $html = '|----', $fieldPri = 'id', $fieldPid = 'uid', $uid = 0, $level = 0) {
        $arr = array();
        foreach ($data as $key) {
            if ($key[$fieldPid] == $uid) {
                $key['level'] = $level + 1;
                $key['html'] = str_repeat($html, $level);
                $arr [] = $key;
                $arr = array_merge($arr, self::Forlevel($data, $html, $fieldPri, $fieldPid, $key[$fieldPri], $key['level']));
            }
        }
        return $arr;
    }

    /**
     * 无限循环多维数组
     * @param array $data 数据
     * @param string $name 多维数组字段名
     * @param string $fieldPri id
     * @param string $fieldPid 上级id
     * @param int $uid     级别
     * @return array 无限循环多维数组
     */
    static public function Forlayer($data, $name = 'child', $fieldPri = 'id', $fieldPid = 'uid', $uid = 0) {
        $arr = array();
        foreach ($data as $key) {
            if ($key[$fieldPid] == $uid) {
                $key [$name] = self::Forlayer($data, $name, $fieldPri, $fieldPid, $key[$fieldPri]);
                $arr[] = $key;
            }
        }
        return $arr;
    }

}
